<?php 
include('includes/confirm.php');
$page_name = friends;
require_once 'classes/Mysql.php';
//require_once 'includes/constants.php';
$mysql = New Mysql();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <?php include('includes/head.php'); ?>
<body>
<div id="wrapper">
    <?php include('includes/header.php'); ?>
    <?php include('includes/menu.php'); ?>
    
    <div id="page">
    	
	      <div id="content">
			<div id="browse_result">
			<table class="sortable" id=anyid cellpadding="0" cellspacing="0">
				<tr>
					<th>Sl. NO.</th>
					<th>User Name</th>
					<th>E-mail</th>
					<th class="unsortable">Delete</th>
				</tr>
				<?php
					if($_POST & !empty($_POST['userid'])){
						$stmt = $mysql->run_query("SELECT id, uname
													FROM user
													WHERE uname = ? 
														AND id IN (SELECT id2 
																		FROM friend
																		WHERE id1=?)", $_POST['userid'], $_SESSION['userid']);
						$stmt->bind_result($id, $user);
						if($stmt->fetch()){
							// add user
							$stmt->close();
							$stmt = $mysql->run_query("DELETE FROM friend 
														WHERE id1 = ? AND id2=?", $_SESSION['userid'], $id);
							echo "<br><br><h4>Deleted Friend: $user<h4>";
						}
						else{
							echo "<br><br><h4>User is not a friend!<h4>";
						}
						$stmt->close();
					}
					$stmt = $mysql->run_query("SELECT uname, email 
												FROM user 
												WHERE id IN ( Select id2
															   FROM friend
															   WHERE id1=?)",$_SESSION['userid']);
					$stmt->bind_result($user, $email);
					$i=1;
					while($stmt->fetch()){
						print "<tr>";
							print "<td>$i</td>";
							print "<td>";
								print "<a href=user_info.php?uname=$user>";
								print $user;
								print "</a>";
							print "</td>";
							print "<td>$email</td>";
							print '<td>';
								print '<form method="post" action="">';
								print '<input type=hidden name="userid"';
								print  "value=$user />";
								print '<input type="submit" name="delete_button" value="Delete"/>';
								print '</form>';
							print '</td>';
						print "</tr>";
						$i++;
					}
					$stmt->close();
					//echo $result->Get('id1');
					echo "</tr>";
					echo "</table>";
				
				?>
			</table>
			</div>
	      </div>
	      <!-- end div#content -->
	      <div id="sidebar">
			<ul>
				<li><h1><a href="friends.php">Browse</a></h1><br></li>
				<li><h1><a href="add_friend.php">Add</a></h1><br></li>
				<li><h1><a href="delete_friend.php">Delete</a></h1><li>
			</ul>
	      </div>
	      <!-- end div#sidebar -->
	      <div style="clear: both; height: 1px"></div>
	   
	  </div>
  
  	</div>
  	<!-- end div#page -->
    <?php include('includes/footer.php'); ?>
</div> <!-- end div#wrapper -->
</body>
</html>
